//
//  QSMessageDetailViewController.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/4/10.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSMessageDetailViewController.h"
#import "QSProblemProcessingViewController.h"
#import "QSEnterpriseVisitViewController.h"
#import "QSEnterpriseSignInController.h"
#import "QSProblemSeportViewController.h"
#import "QSHomeInterface.h"
#import "QSProblemDetailViewController.h"
#import "QSSubmitProcessingViewController.h"
#import "QSProblemReprotInterface.h"
@interface QSMessageDetailViewController ()
@property (nonatomic, strong)UIImageView *leftImage;

@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UILabel *textLabel;
@property (nonatomic, strong)UIButton *jumpButton;
@property (nonatomic, strong)UILabel *dateLabel;

@end

@implementation QSMessageDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"消息提醒";
    NSLog(@"%ld", _model.type);
    if (!_model.isRead) {
        [self setMessageRead];//设置为已读
    }
    [self configSubView];
}

- (void)setMessageRead{
    [QSHomeInterface getMessageReadWithId:_model.id andBlock:^(ResponseMessage * _Nonnull rspStatusAndMessage, NSDictionary * _Nonnull result) {
        if (rspStatusAndMessage.code == kResponseSuccessCode) {

        }
    }];
}
- (void)configSubView{
    [self.view addSubview:self.leftImage];
    [self.view addSubview:self.titleLabel];
    [self.view addSubview:self.textLabel];
    [self.view addSubview:self.jumpButton];
    [self.view addSubview:self.dateLabel];
    _textLabel.numberOfLines = 0;
    [_leftImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(kRealValue(15));
        make.top.equalTo(self.view).offset(kRealValue(23));
        make.width.height.offset(kRealValue(36));
    }];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kRealValue(20));
        make.left.equalTo(self.leftImage.mas_right).offset(kRealValue(13));
    }];
    [_textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(kRealValue(8));
        make.left.equalTo(self.leftImage.mas_right).offset(kRealValue(13));
//        make.right.equalTo(self.view).offset(-kRealValue(13));
    }];
    [_jumpButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.textLabel.mas_right).offset(0);
        make.centerY.equalTo(self.textLabel);
    }];
    [_dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftImage.mas_right).offset(kRealValue(13));
        make.top.equalTo(self.textLabel.mas_bottom).offset(kRealValue(13));
    }];
}

#pragma mark - 懒加载
- (UIImageView *)leftImage{
    if (!_leftImage) {
        _leftImage = [[UIImageView alloc]init];
        _leftImage.layer.cornerRadius = 18;
    }
    return _leftImage;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [UILabel labelWithName:@"问题处理" font:[UIFont QSFontOfSize14] textColor:[UIColor blackColor] textAlignment:NSTextAlignmentLeft];
    }
    return _titleLabel;
}
- (UILabel *)textLabel{
    if (!_textLabel) {
        _textLabel = [UILabel labelWithName:@"您有一条消息待处理，" font:[UIFont QSFontOfSize12] textColor:[UIColor blackColor] textAlignment:NSTextAlignmentLeft];
    }
    return _textLabel;
}

- (UIButton *)jumpButton{
    if (!_jumpButton) {
        _jumpButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_jumpButton setTitle:@"前往查看" forState:UIControlStateNormal];
        [_jumpButton addTarget:self action:@selector(jumpAction:) forControlEvents:UIControlEventTouchUpInside];
        _jumpButton.titleLabel.font = [UIFont QSFontOfSize12];
    }
    return _jumpButton;
}

- (UILabel *)dateLabel{
    if (!_dateLabel) {
        _dateLabel = [UILabel labelWithName:@"2020-4-4 17:00" font:[UIFont QSFontOfSize12] textColor:[UIColor lightGrayColor] textAlignment:NSTextAlignmentLeft];
    }
    return _dateLabel;
}

- (void)setModel:(QSMessageDetailModel *)model{
    _model = model;
    //类型  1亩均 2问题 3走访 4签到
    if (_model.type == 5) {
        self.leftImage.image = [UIImage imageNamed:@"ic_message_problem"];
        self.titleLabel.text = @"问题上报";
    }else if(model.type == 2){
        self.leftImage.image = [UIImage imageNamed:@"home_problemdeal"];
        self.titleLabel.text = @"问题处理";
        [self.jumpButton setTitle:@"前往处理" forState:UIControlStateNormal];
    }else if (model.type == 3){
        self.leftImage.image = [UIImage imageNamed:@"ic_message_visit"];
        self.titleLabel.text = @"企业走访";
    }else if (model.type == 4){
        self.leftImage.image = [UIImage imageNamed:@"home_signin"];
        self.titleLabel.text = @"驻企签到";
    }else{
        self.leftImage.image = [UIImage imageNamed:@"ic_message_mujun"];
        self.titleLabel.text = @"亩均审批";
        self.jumpButton.hidden = YES;
    }
    self.textLabel.text = [NSString stringWithFormat:@"%@ ",  model.title];
    if (!model.skipStateApp) {
        self.jumpButton.hidden = YES;
        if (model.skipInfoApp) {
            self.textLabel.text = [NSString stringWithFormat:@"%@, %@",  model.title, model.skipInfoApp];

        }else{
            self.textLabel.text = [NSString stringWithFormat:@"%@",  model.title];
        }
    }
    NSString *labelText = [NSDate getTimeFromTimestampWithStr:model.createTime format:@"yyyy-MM-dd HH:mm"];
    self.dateLabel.text = labelText;
}
- (void)jumpAction:(UIButton *)sender{
    if (_model.type == 1) {

    }else if(_model.type == 2){
        [self getProblemDetailWithJump];//请求此问题详情根据状态进行跳转
    }else if (_model.type == 3){
        QSEnterpriseVisitViewController *vc = [QSEnterpriseVisitViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (_model.type == 4){
        QSEnterpriseSignInController *vc = [QSEnterpriseSignInController new];
        [self.navigationController pushViewController:vc animated:YES];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [vc selectIndex:1];
        });
    }else{
//        QSProblemSeportViewController *vc = [QSProblemSeportViewController new];
//        [self.navigationController pushViewController:vc animated:YES];
        //跳转详情
        QSProblemDetailViewController *vc = [QSProblemDetailViewController new];
        vc.problemId = _model.typeKey;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
//请求此问题当前详情  得到状态进行跳转操作

- (void)getProblemDetailWithJump{
    [QSAppKeyWindow showIndeterminateHudWithText:@"加载中"];
    [QSProblemReprotInterface getApiProblemDetailWithID:_model.typeKey.integerValue andBlock:^(ResponseMessage * _Nonnull rspStatusAndMessage, QSProblemDetailModel * _Nonnull result) {
        [QSAppKeyWindow hideHud];
        if (rspStatusAndMessage.code == kResponseSuccessCode) {
            if (result.state.integerValue == 2) {//只有待处理跳处理页
                QSSubmitProcessingViewController *vc = [QSSubmitProcessingViewController new];
                vc.problemId = self.model.typeKey;
                vc.isMessagePush = YES;
                [self.navigationController pushViewController:vc animated:YES];
            }else{ //跳详情页
                QSProblemDetailViewController *vc = [QSProblemDetailViewController new];
                vc.problemId = self.model.typeKey;
                [self.navigationController pushViewController:vc animated:YES];
            }
        }
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
